import settings from '../utils/setting'
export default {
    aryVal(ary, id, key) {
        return settings.globalData[ary].find(t => t.id == id)[key||'text']
    },
    copyText(text) {
        var input = document.createElement('input');
        input.value = text;
        document.body.appendChild(input);
        input.select();
        document.execCommand('copy');
        document.body.removeChild(input);
        ElMessage({ message: '复制成功！', type: 'success', duration: 2 * 1000 })
    },
    dateFormat(d,fmt) {
        var o = { 
            "M+" : d.getMonth()+1,                 //月份 
            "d+" : d.getDate(),                    //日 
            "h+" : d.getHours(),                   //小时 
            "m+" : d.getMinutes(),                 //分 
            "s+" : d.getSeconds(),                 //秒 
            "q+" : Math.floor((d.getMonth()+3)/3), //季度 
            "S" : d.getMilliseconds()             //毫秒 
        }; 
        if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (d.getFullYear()+"").substr(4 - RegExp.$1.length)); 
        }
        for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    },
    dateAddMinute(d,minute,fmt){
        let nd = new Date(d.getTime()+minute*60*1000);
        if(fmt)
            return this.dateFormat(nd, fmt)
        return nd
    },
    dateAddHour(d,hour,fmt){
        let nd = new Date(d.getTime()+hour*60*60*1000);
        if(fmt)
            return this.dateFormat(nd, fmt)
        return nd
    },
    dateAddDay(d,day,fmt){
        let nd = new Date(d.getTime()+day*24*60*60*1000)
        if(fmt)
            return this.dateFormat(nd, fmt)
        return nd
    },
    dateAddMonth(d,month,fmt){

    },
    dateAddYear(d,year,fmt){

    },
    removeHTML(str, len){
        str = str.replace(/<[^>]+>/g,' ');
        if(len){
            if(str.length > len){
                str = str.substr(0,len) + "...";
            }
        } 
        return str;
    },
    convertSize(l) {
        if(l<1024)
            return l + "B";
        if (l<1024*1024)
            return (l/1024.0).toFixed(1) + "KB";
        if (l<1024*1024*1024)
            return (l/1024/1024.0).toFixed(1) + "MB";
    },
    position(point, params, dom, rect, size) {
		var x = 0; // x坐标位置
		var y = 0; // y坐标位置
		// 当前鼠标位置
		var pointX = point[0];
		var pointY = point[1];
	
		// 提示框大小
		var boxWidth = size.contentSize[0];
		var boxHeight = size.contentSize[1];
	
		// boxWidth > pointX 说明鼠标左边放不下提示框
		if (boxWidth > pointX) {
			x = 5;
		} else { // 左边放的下
			x = pointX - boxWidth;
		}
	
		// boxHeight > pointY 说明鼠标上边放不下提示框
		if (boxHeight > pointY) {
			y = 5;
		} else { // 上边放得下
			y = pointY - boxHeight;
		}
	
		return [x, y];
	},
    // 判断某元素是否有滚动条 id
    hasScrolled(id, direction = "vertical") {
        let el = document.getElementById(id)
        if(direction === "vertical") {
            return el.scrollHeight > el.clientHeight;
        }else if(direction === "horizontal") {
            return el.scrollWidth > el.clientWidth;
        }
    },
    xlsImport(proxy,filename) {
      return new Promise((resolve, reject) => {
          proxy.$ajax({
            baseURL:proxy.$setting.globalUrl.base,
            url: '/zh8848/xlsImport',
            data: {filename:filename},
            bfLoading: true
          }).then(d => {
              resolve(d.msg.list)
          }).catch(e => {
              reject(e)
          })
      })
    },
    getAssetsFile(url){
        return new URL(`../assets/${url}`, import.meta.url).href
     }
}